21. Practice: While Loops
If you already have programming experience, feel free to skip any exercises that you don't feel are necessary for you.
Practice: Factorials with While Loops
Find the factorial of a number using a
while
loop.
A factorial of a whole number is that number multiplied by every whole number between itself and 1. For example, 6 factorial (written "6!") equals 6 x 5 x 4 x 3 x 2 x 1 = 720. So 6! = 720.
We can write a
while
loop to take any given number and figure out what its factorial is.
Example: If
number
is 6, your code should compute and print the
product
, 720.
Start Quiz:
# number to find the factorial of
number = 6
# start with our product equal to one
product = 1
# track the current number being multiplied
current = 1
# write your while loop here
# multiply the product so far by the current number
# increment current with each iteration until it reaches number
# print the factorial of number
print(product)
Practice: Factorials with For Loops
Now use a
for
loop to find the factorial!
It will now be great practice for you to try to revise the code you wrote above to find the factorial of a number, but this time, using a
for
loop. Try it in the code editor below!
Start Quiz:
# number to find the factorial of
number = 6
# start with our product equal to one
product = 1
# write your for loop here
# print the factorial of number
print(product)